package io.framework.modules.air.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.common.utils.DateUtils;
import io.framework.modules.air.dao.AirCheckExplainDao;
import io.framework.modules.air.dao.AirDao;
import io.framework.modules.air.dao.AirDayNumberDao;
import io.framework.modules.air.dao.AirNdChangeDao;
import io.framework.modules.air.dto.AirDTO;
import io.framework.modules.air.dto.AirExDTO;
import io.framework.modules.air.entity.AirEntity;
import io.framework.modules.air.service.AirService;
import io.framework.modules.station.dto.StationDTO;
import io.framework.modules.station.service.StationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Gujq email
 * @since 1.0.0 2020-05-20
 */
@Service
public class AirServiceImpl extends CrudServiceImpl<AirDao, AirEntity, AirDTO> implements AirService {

    @Autowired
    private AirDayNumberDao airDayNumberDao;

    @Autowired
    private AirCheckExplainDao airCheckExplainDao;

    @Autowired
    private AirNdChangeDao airNdChangeDao;

    @Autowired
    private StationService stationService;

    @Autowired
    private AirDao airDao;

    @Override
    public QueryWrapper<AirEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AirEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public PageData<AirDTO> page(Map<String, Object> params) {

        List<Integer> typeList = Arrays.asList(1, 2);
        params.put("type", typeList);

        IPage<AirEntity> pages = getPage(params, Constant.CREATE_DATE, false);

        List<AirEntity> list = baseDao.getAirDTOIOnfoByParams(params);

        return getPageData(list, pages.getTotal(), AirDTO.class);
    }

    @Override
    public List<Map<String, Object>> aqiCalendar(Map<String, Object> params) {
        String dateStr = "";
        if (params.containsKey("date")) {
            dateStr = params.get("date").toString();
        }
        if (StringUtils.isBlank(dateStr)) {
            dateStr = DateUtils.format(new Date(), DateUtils.DATE_MONTH_PATTERN);
        }
        dateStr += "-01";
        params.put("startDate", DateUtils.getMonthBegin(dateStr));
        params.put("endDate", DateUtils.getMonthEnd(dateStr));
        return baseDao.aqiCalendar(params);
    }

    @Override
    public List<Map<String, Object>> aqiHistoryChange(Map<String, Object> params) {
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        Integer type = Integer.parseInt(params.get("type").toString());
        String startDateStr = params.get("startDate").toString();
        String endDateStr = params.get("endDate").toString();
        if (StringUtils.isEmpty(startDateStr) || StringUtils.isEmpty(endDateStr)) {
            //默认最近一周
            params = DateUtils.getDaySevenRange();
        }
        if (type != 1) {
            params.put("type", 3);
            params.put("stationId", "");
        } else {
            String stationId[] = params.get("stationId").toString().replaceAll(" ", "").split(",");
            for (String id : stationId) {
                //不需要区分 国控/省控/市控
                params.put("type", "");
                Map<String, Object> map = new HashMap<>();
                Long sid = Long.parseLong(id);
                params.put("stationId", sid);
                StationDTO stationDTO = stationService.get(sid);
                if (stationDTO != null) {
                    params.put("factor", "AQI");
                    List<Map<String, Object>> list = baseDao.airFactorChange(params);
                    map.put("stationName", stationDTO.getName());
                    map.put("listData", list);
                    resultMapList.add(map);
                }
            }
        }
        return resultMapList;
    }

    @Override
    public List<Map<String, Object>> factorHistoryChange(Map<String, Object> params) {
        Integer type = Integer.parseInt(params.get("type").toString());
        String startDateStr = params.get("startDate").toString();
        String endDateStr = params.get("endDate").toString();
        if (StringUtils.isEmpty(startDateStr) || StringUtils.isEmpty(endDateStr)) {
            //默认最近一周
            params = DateUtils.getDaySevenRange();
        }
        if (type != 1) {
            params.put("type", 3);
            params.put("stationId", "");
        }
        return baseDao.airFactorChange(params);
    }

    @Override
    public AirDTO getAirDTOIOnfoByParams(Map<String, Object> params) {

        List<AirEntity> list = baseDao.getAirDTOIOnfoByParams(params);
        AirEntity entity = new AirEntity();
        if (CollectionUtils.isNotEmpty(list)) {
            entity = list.get(0);
        } else {
            return null;
        }
        return ConvertUtils.sourceToTarget(entity, AirDTO.class);
    }

    @Override
    public AirDTO getAirDTOIOnfoByParam(Map<String, Object> params) {

        AirEntity entity = baseDao.getAirDTOIOnfoByParam(params);

        return ConvertUtils.sourceToTarget(entity, AirDTO.class);
    }


    @Override
    public List<Map<String, Object>> getAirStationInfoSort(Map<String, Object> params) {

        List<Map<String, Object>> list = baseDao.getAirStationInfoSort(params);

        return list;
    }

    @Override
    public Boolean checkData(Integer type, String month) {
        Boolean flag = false;
        List<AirEntity> list = baseDao.checkData(type, month);
        if (CollectionUtils.isNotEmpty(list)) {
            flag = true;
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfoByMonth(String month) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("month", month);
        baseDao.deleteInfoByDate(params);
        airDayNumberDao.deleteInfoByDate(params);
        airCheckExplainDao.deleteInfoByDate(params);
        airNdChangeDao.deleteInfoByDate(params);
    }

    @Override
    public AirEntity getByStationIdAndDate(AirEntity airEntity) {
        return baseDao.selectOne(new QueryWrapper<AirEntity>()
                .eq("station_id", airEntity.getStationId())
                .eq("date", airEntity.getDate()));
    }

    @Override
    public List<Map<String, Object>> aqiHistoryPm(Map<String, Object> params) {

        List<Map<String, Object>> list = baseDao.aqiHistoryPm(params);

        return list;
    }

    @Override
    public List<AirExDTO> getAirRecentTime(String createTime) {
        return airDao.getAirRecentTime(createTime);
    }


}
